package com.sctu.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.EMSApp.FamousOneInfoActivity;
import com.sctu.EMSApp.R;
import com.sctu.adapter.FoodAdapter;
import com.sctu.adapter.PlayFoodListviewAdapter;
import com.sctu.adapter.ViewPagerAdapter;
import com.sctu.bean.FoodHead;
import com.sctu.bean.PlayFoodListOne;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhu on 2015/11/7.
 */
public class PlayFoodFragment extends Fragment {
    private View view;
    private ListView listView;
    //存放添加到viewpager的list
    private List<View> listviews;
    private ViewPager viewPager;
    private ImageView imageView;
    private List<ImageView> listimageView = new ArrayList<ImageView>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (view == null){
            view = inflater.inflate(R.layout.fragment_play_food, container, false);
            viewPager = (ViewPager) view.findViewById(R.id.play_food_viewpager);
            listView = (ListView) view.findViewById(R.id.food_list);
            initHeading();
            initListView();
        }

        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        return view;

    }

    private void initHeading() {
        //获取网络数据
        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_FOOD_HEAD, listenerhead, errorListener);
    }

    Response.Listener<String> listenerhead = new Response.Listener<String>() {
        @Override
        public void onResponse(String headJson) {
            Log.e("json", headJson);

            FoodHead foodHead = JSON.parseObject(headJson, FoodHead.class);
            List<FoodHead.Results> fr = foodHead.getResults();


            listviews = new ArrayList<View>();
            for (int i = 0; i < fr.size(); i++) {
                View itemviewpagerview =  LayoutInflater.from(getActivity()).inflate(R.layout.item_viewpager_paly_food, null);
                imageView = (ImageView) itemviewpagerview.findViewById(R.id.item_viewpager_paly_food_image);
                //获取网络图片并加载到相应的view上面
                GetVolleyHttp.getInstance(getActivity()).
                        setImageLoader(imageView, R.drawable.error_image, R.drawable.error_image, UrlConfig.URL + fr.get(i).getPath());
                //添加到image集合中
                listviews.add(itemviewpagerview);

            }

            viewPager.setAdapter(new ViewPagerAdapter(listviews));

            /**
             * 动态的对小圆点进行控制
             */
            LinearLayout mainLayout = (LinearLayout) view.findViewById(R.id.play_food_head_Relative_spot);

            for (int i = 0; i < fr.size(); i++) {
                LinearLayout linearLayout = new LinearLayout(getActivity());
                LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                //设置每个小圆点距离左边的间距

                margin.setMargins(10, 0, 0, 0);
                imageView = new ImageView(getActivity());

                //设置每个小圆点的宽高
                imageView.setLayoutParams(new ViewGroup.LayoutParams(10, 10));
                listimageView.add(imageView);
                if (i == 0) {
                    // 默认选中第一张图片
                    listimageView.get(i)
                            .setBackgroundResource(R.drawable.scenic_tour_round_after_click_click);
                } else {
                    //其他图片都设置未选中状态
                    listimageView.get(i)
                            .setBackgroundResource(R.drawable.scenic_tour_round_after_click_unclick);
                }
                linearLayout.addView(listimageView.get(i), margin);
                mainLayout.addView(linearLayout);
            }

            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {

                }

                /**
                 * 此方法是页面跳转完后得到调用，arg0是你当前选中的页面的Position（位置编号）
                 * @param position
                 */
                @Override
                public void onPageSelected(int position) {
                    //遍历让当前选中图片下的小圆点设置颜色
                    for (int i = 0; i < listimageView.size(); i++) {
                        listimageView.get(i)
                                .setBackgroundResource(R.drawable.scenic_tour_round_after_click_click);

                        if (position != i) {
                            listimageView.get(i)
                                    .setBackgroundResource(R.drawable.scenic_tour_round_after_click_unclick);
                        }
                    }
                }

                /**
                 * arg0 == 1的时辰默示正在滑动，arg0 == 2的时辰默示滑动完毕了，arg0 == 0的时辰默示什么都没做。
                 */
                @Override
                public void onPageScrollStateChanged(int arg0) {

                }
            });
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(getActivity(), "网络出除了点小问题", Toast.LENGTH_LONG).show();
        }
    };


    private void initListView() {

        //发送网络请求

        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_FOOD_LIST, listenerlist, errorListener);




    }

    Response.Listener<String> listenerlist = new Response.Listener<String>() {

        @Override
        public void onResponse(String listjson) {
            Log.e("json", listjson);
            PlayFoodListOne playFoodListOne = JSON.parseObject(listjson, PlayFoodListOne.class);
            listView.setAdapter(new PlayFoodListviewAdapter(getActivity(), playFoodListOne.getResults()));


        }
    };


}
